import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现 Callable
 */
public class ThredDemo8 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable();
        // 中间类来接收创建新线程的返回值
        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
        // 创建新线程
        Thread thread = new Thread(futureTask);
        // 启动线程
        thread.start();
        // 接收新线程执行的结果
        int result = futureTask.get();
        System.out.println("新线程执行的结果:" + result);
    }
}

/**
 * Callable后面的泛型里面可以是任意类型
 */
class MyCallable implements Callable<Integer> {
//生成随机数的方法
    @Override
    public Integer call() throws Exception {
        // 生成随机数 0-9
        int randomNum = new Random().nextInt(10);
        System.out.println(Thread.currentThread().getName() +
                "——随机数：" + randomNum);
        return randomNum;
    }
}